﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Hr.IBLL;
using Newtonsoft.Json;
using Hr.BLLContainer;
using System.Data.Entity;
using Newtonsoft.Json.Linq;
using Hr.BLL;
using Hr.Model;
using System.Web.UI;
using System.Web;
using HR.Areas.HelpPage.ModelDescriptions;
using HR.HrHelper;
using System.Reflection;
using System.Collections;

namespace HR.Areas.letao.Controllers.Role
{
    public class RoleMangeController : ApiController
    {
        // 全局变量，必须的
        //导入BLL层
        protected RoleService RoleName = new RoleService();
        int code = 0;
        string msg = "success";

        /// <summary>
        /// 获取所有角色信息API
        /// </summary>
        /// <returns>角色信息，Json数据</returns>
        [HttpGet]
        public JObject GetRoleNameList()
        {
            Dictionary<string, object> dt = new Dictionary<string, object>()
            {
                {"code",code},
                {"msg",msg },
                {"data",RoleName.GetRoleNameAll()}
            };
            return (JObject)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(dt));
        }

        /// <summary>
        /// 根据角色ID查询单个角色或者根据角色名称进行模糊查询多个角色
        /// </summary>
        /// <param name="pk_Name"></param>
        /// <param name="RoleName_CHS></param>
        /// <returns></returns>
        [HttpGet]
        public JObject GetRoleName(string NameID, string Type)
        {
            object data = "";
            if (Type.Equals("SelName"))
            {
                //模糊查询
                data = NameID != null ? RoleName.SelRoleName(NameID) : RoleName.GetRoleNameAll();
            }
            else
            {
                int id = int.Parse(NameID);
                data = RoleName.GetRoleName(id);
            }
            Dictionary<string, object> dt = new Dictionary<string, object>()
            {
                {"code",code},
                {"msg",msg },
                {"data",data}
            };
            return (JObject)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(dt));
        }
        /// <summary>
        /// 添加、修改角色信息
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public JObject UpdateRoleName()
        {
            System.Collections.Specialized.NameValueCollection RequestForm = HttpContext.Current.Request.Form;
            // 调用jsonhelper帮助类转换成对象
            Role_Name r = JsonHelper.DeserializeJsonToObject<Role_Name>(RequestForm["Role"]);
            string type = RequestForm["Type"];
            if (type.Equals("Edit"))
            {
                Role_Name SelRo = RoleName.GetRoleName(r.pk_Name);
                msg = "更新成功";
                if (SelRo == null)
                {
                    code = 1;
                    msg = "角色不存在";
                }
                else
                {
                    //两个对象对比是否相等
                    if (ModelHelper.ReflectModel(r, SelRo))
                    {
                        msg = "无任何更新";
                    }
                    else
                    {
                        if (!RoleName.UpdateRoleName(r, type))
                        {
                            code = 1;
                            msg = "更新失败";
                        }
                    }
                }
            }
            else
            {
                Role_Name SelRo = RoleName.GetModels(n => n.RoleName_CHS.Equals(r.RoleName_CHS)).FirstOrDefault();
                msg = "添加成功";
                //判断是否有重复
                if (SelRo != null)
                {
                    code = 1;
                    msg = "角色已存在！";
                }
                else
                {
                    if (!RoleName.UpdateRoleName(r, type))
                    {
                        code = 1;
                        msg = "添加失败";
                    }

                }

            }
            Dictionary<string, object> dt = new Dictionary<string, object>()
            {
                {"code",code},
                {"msg",msg },
                {"data",RoleName.GetModels(n=>true)}
            };
            return (JObject)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(dt));
        }

        /// <summary>
        /// 删除角色信息，根据角色ID
        /// </summary>
        /// <param name="pk_Name"></param>
        /// <returns></returns>
        [HttpPost]
        public JObject DelRoleName()
        {
            System.Collections.Specialized.NameValueCollection RequestForm = HttpContext.Current.Request.Form;
            int pk_Name = int.Parse(RequestForm["pk_Name"].ToString());
            Role_Name r = RoleName.GetRoleName(pk_Name);
            msg = "删除成功";
            if (r != null)
            {
                if (!RoleName.DelRoleName(pk_Name))
                {
                    //失败
                    code = 1;
                    msg = "删除失败";
                }
            }
            else
            {
                code = 1;
                msg = "角色不存在";
            }

            Dictionary<string, object> dt = new Dictionary<string, object>()
            {
                {"code",code},
                {"msg",msg }
            };
            return (JObject)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(dt));
        }

        /// <summary>
        /// 获取所有菜单信息,通过角色ID获取角色已开启权限菜单信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public JObject GetMenuList(int pk_Name)
        {
            Dictionary<string, object> dt = new Dictionary<string, object>()
            {
                {"code",code},
                {"msg",msg },
                {"data",RoleName.GetMenuList(pk_Name) }
            };
            return (JObject)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(dt));
        }

        [HttpPost]
        public JObject UpdateRoleCheck()
        {
            System.Collections.Specialized.NameValueCollection RequestForm = HttpContext.Current.Request.Form;
            int pk_Name = int.Parse(RequestForm["pk_Name"].ToString());
            List<int> CheckDt = new List<int>();
            if (RequestForm["CheckDt"] != null)
            {
                CheckDt = RequestForm["CheckDt"].Split(',').Select(n => int.Parse(n)).ToList();
            }
            //查询角色是否存在
            Role_Name r = RoleName.GetRoleName(pk_Name);
            msg = "更新成功";
            if (r != null)
            {
                List<int> CheckNowDt = (RoleName.GetMenuList(pk_Name)["CheckDt"] as IQueryable<int?>).Select(n => n.Value).ToList();
                if (ModelHelper.ReflectModel(CheckDt, CheckNowDt))
                {
                    msg = "无更新";
                }
                else
                {
                    //执行修改菜单操作
                    if (!RoleName.UpdateRoleCheck(pk_Name, CheckDt))
                    {
                        code = 1;
                        msg = "更新失败";
                    }
                }
            }
            else
            {
                code = 1;
                msg = "更新失败，角色不存在";
            }
            Dictionary<string, object> dt = new Dictionary<string, object>()
            {
                {"code",code},
                {"msg",msg }
            };
            return (JObject)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(dt));
        }
    }
}

